package com.iurmfy.test;

import com.iurmfy.Data.UserData;
import com.iurmfy.deskpet.PetFrame;

import javax.swing.*;
import java.awt.*;

public class UserFrame
{
    private static final JFrame frame = new JFrame("desktop pet");

    private UserFrame(){}

    // 初始化用户界面
    public static void initUserFrame()
    {
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /*
         * ui 初始化
         */
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        SettingFrame.initFrame();

        // 添加一个新的人物
        JButton button1 = new JButton("add");
        // 测试是否有数据
        JButton button2 = new JButton("test");
        // 兴建
        JButton button3 = new JButton("create");

        button1.addActionListener(e ->
        {
            SettingFrame.ShowFrame();
        });
        button2.addActionListener(e->
        {
            System.out.println("m:" + UserData.multiple+ "\n" + "p:" + UserData.path);
        });
        button3.addActionListener(e->
        {
            Image image = new ImageIcon(UserData.path).getImage();
            PetFrame pet = new PetFrame(image, UserData.multiple);
            pet.showPet();
        });

        panel.add(button1);
        panel.add(button2);
        panel.add(button3);

        frame.add(panel);
        frame.setVisible(true);
    }


}

class SettingFrame
{
    private static final JFrame frame = new JFrame();

    private SettingFrame(){}

    public static void initFrame()
    {
        frame.setSize(600,400);
        frame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();

        JLabel label1 = new JLabel("倍数");
        JLabel label2 = new JLabel("地址");

        JTextField text1 = new JTextField(20);
        JTextField text2 = new JTextField(20);

        JButton button = new JButton("确认");

        panel.setLayout(new GridLayout(3,2));

        panel.add(label1);
        panel.add(text1);
        panel.add(label2);
        panel.add(text2);

        panel.add(button);

        button.addActionListener(e ->
            {
                //UserData.multiple = Integer.getInteger(text1.getText());
                UserData.multiple = Integer.parseInt(text1.getText());
                UserData.path = text2.getText();

                frame.setVisible(false);
            });

        frame.add(panel);
    }

    public static void ShowFrame()
    {
        frame.setVisible(true);
    }
}
